package com.chrray.position.util.user;


import com.chrray.position.entity.position.SysUser;
import com.chrray.position.mapper.position.SysUserMapper;
import com.chrray.position.util.redis.RedisUtils;
import com.chrray.position.util.request.RequestUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 * @Data: Create in 2019-06-12 09:33
 * @Modified By:
 */
@Component
@Slf4j
public class GetSysUserUtil {
    @Resource
    private SysUserMapper sysUserMapper;

    @Autowired
    private RedisUtils redisUtils;

    public static GetSysUserUtil getUserInfoUtil;




    @PostConstruct
    public void init() {
        getUserInfoUtil = this;
    }

    public static SysUser getUserInfo(HttpServletRequest request){
        String userId =  getsUserId(request);
        if(StringUtils.isEmpty(userId)){
            return null;
        }
        SysUser userInfo =getUserInfoUtil.sysUserMapper.selectByPrimaryKey(userId);
        return userInfo;
    }


    public static String getsUserId(HttpServletRequest request){
        String token = RequestUtils.getToken(request);
        return getsUserId(token);
    }

    public static String getsUserId(String token){
        Object userId=getUserInfoUtil.redisUtils.get(token);
        if(ObjectUtils.isEmpty(userId)){
            return  "";
        }
        return userId.toString();
    }
}

